package com.market.util;

import java.util.Formatter;

public class StringUtil {

	StringBuilder sBuilder;
	Formatter formatter;

	public enum TYPE {
		STRING, INTEGRAL, FLOAT
	}

	public StringUtil() {
		sBuilder = new StringBuilder();
		formatter = new Formatter(sBuilder);
	}

	public StringUtil(StringBuilder sBuilder) {
		this.sBuilder = sBuilder;
		formatter = new Formatter(sBuilder);
	}
	
	/**
	 * if cent is "1", "2", turn it to "0"; if cent is "3", "4", turn it into "5"
	 * if cent is "6", "7", turn it into "5"; if cent is "8", "9", turn it into "10" 
	 * @param s
	 */
	public static String formatCurrency(String s){
		String result = "";
		String cent = s.substring(s.length() -1);
		
		
		
		if(cent.equals("8") || cent.equals("9")){
			cent = "0";
			result = s.substring(s.length() - 2, s.length() - 1);
			Integer tenCent = Integer.parseInt(result) + 1;
			result = s.substring(0, s.length() - 2) + tenCent.toString() + cent;
		}else{
			if(cent.equals("1") || cent.equals("2")){
				cent = "0";
			}
			if(cent.equals("3") || cent.equals("4")){
				cent = "5";
			}
			if(cent.equals("6") || cent.equals("7")){
				cent = "5";
			}
			result = s.substring(0, s.length() -1) + cent;
		}
		return result;
	}
	
	
	
	public Formatter format(String format, Object... args) {
		return formatter.format(format, args);
	}

	public void format(String original, boolean leftAlign, int minWidth,
			int maxWidth) {
		int ratio = original.getBytes().length / original.length();
		switch (ratio) {

		case 2:
			if (original.length() > minWidth / 2) {
				original = original.substring(0, minWidth / 2);
			}
			formatter.format(this.genFormatString(TYPE.STRING, leftAlign,
					minWidth, maxWidth), original);
			break;
		case 3:
			if (original.length() > minWidth / 4) {
				original = original.substring(0, minWidth / 4);
			}
			minWidth = minWidth - original.length() * 3;

			maxWidth = minWidth;

			formatter.format(this.genFormatString(TYPE.STRING, leftAlign,
					minWidth, maxWidth), original);
			break;
		default:
			formatter.format(this.genFormatString(TYPE.STRING, leftAlign,
					minWidth, maxWidth), original);
			break;
		}
	}

	public void format(String original, int width) {
		this.format(original, false, width, width);
	}

	public void append(String original) {
		int ratio = original.getBytes().length / original.length();
		int width = 0;
		switch (ratio) {

		case 2:
			width = original.length() * 2;
			break;
		case 3:
			width = original.length() * 4;
			break;
		default:
			width = original.length();
			break;
		}
		this.format(original, width);
	}

	public void format(String original, boolean leftAlign) {
		int ratio = original.getBytes().length / original.length();
		int width = 0;
		switch (ratio) {

		case 2:
			width = original.length() * 2;
			break;
		case 3:
			width = original.length() * 4;
			break;
		default:
			width = original.length();
			break;
		}
		this.format(original, leftAlign, width, width);
	}

	public void format(double original, boolean leftAlign, int width,
			int precision) {
		formatter.format(
				this.genFormatString(TYPE.FLOAT, leftAlign, width, precision),
				original);
	}

	public void format(double original, int width, int precision) {
		formatter.format(
				this.genFormatString(TYPE.FLOAT, false, width, precision),
				original);
	}

	public void format(int original, boolean leftAlign, int width) {
		formatter.format(
				this.genFormatString(TYPE.INTEGRAL, leftAlign, width, width),
				original);
	}

	public void format(int original, int width) {
		formatter.format(
				this.genFormatString(TYPE.INTEGRAL, false, width, width),
				original);
	}

	private String genFormatString(TYPE type, boolean leftAlign, int width,
			int precision) {
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append('%');
		if (leftAlign) {
			sBuilder.append('-');
		}
		sBuilder.append(width);
		sBuilder.append('.');
		sBuilder.append(precision);
		switch (type) {
		case INTEGRAL:
			sBuilder.append('d');
			break;
		case FLOAT:
			sBuilder.append('f');
			break;
		default:
			sBuilder.append('s');
		}

		return sBuilder.toString();
	}

	public void newLine(int number) {
		for (int i = 0; i < number; i++) {
			sBuilder.append('\n');
		}
	}

	public String toString() {
		return sBuilder.toString();
	}

	/**
	 * This method format original String to specific length, if the original
	 * String is shorter than length, space will be added; if the original
	 * String is longer than length, it will be trim to specific length.
	 * 
	 * @param original
	 * @param length
	 * @return result
	 */
	public static String format(String original, Integer length) {
		if (original.getBytes().length > length) {
			original = original.substring(0, length - 1);
		} else {
			if (original.getBytes().length < length) {
				StringBuffer originalBuffer = new StringBuffer(original);
				for (int i = 0; i < length - original.getBytes().length; i++) {
					originalBuffer.append(" ");
				}
				original = originalBuffer.toString();

			}
		}
		return original;
	}

	/**
	 * This method format original String to specific length plus endSymbol, if
	 * the original String is shorter than length, space will be added; if the
	 * original String is longer than length, it will be trim to specific
	 * length.
	 * 
	 * @param original
	 * @param length
	 * @param endSymbol
	 * @return
	 */
	public static String format(String original, Integer length,
			String endSymbol) {
		original = format(original, length);
		return original + endSymbol;
	}

}
